<template>
  <div>
 <div class="dialogPop">
      <el-dialog :visible.sync="addData.dialogAdd" 
      :before-close="handleClose" size="tiny">
        <p class="dialog-title">{{addData.popTitle}}</p>
        <el-form :model="addData" 
        :rules="rulesAdd" ref="addData" label-width="124px">
                <el-row :gutter="20">
                    <el-col :span="20">
                    <el-form-item label="敏感词：" prop="word">
                        <el-input v-model="addData.word" placeholder="请输入敏感词"></el-input>
                    </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="20">
                    <el-form-item label="替换词：" prop="changeWord">
                        <el-input v-model="addData.changeWord" placeholder="请输入替换词"></el-input>
                    </el-form-item>
                    </el-col>
                </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <p class="btn">
            <el-button type="primary" @click="addInfor('addData')">确 认</el-button>
            <el-button @click="resetFormdelDialogAdd('addData')">取 消</el-button>
          </p>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { addSensitiveWords, updateSensitiveWords} from 'api/system';
export default {
    props: {
        addData: {
            default() {
                return {
                    dialogAdd: false // 默认不显示
                }
            },
            required: true
        }
    },
    data() {
        return {
            rulesAdd: {
                word: [{ required: true, message: '请输入敏感字', trigger: 'blur' }],
                changeWord: [{ required: true, message: '请输入替换词', trigger: 'blur' }]
            }
        }
    },
    created() {
        //console.log(1);
    },
    methods: {
      // 弹窗的确定按钮
        addInfor(addData) {
            this.$refs[addData].validate((valid) => {
                if (valid) {// 验证通过
                    let sensitiveWord=this.addData.word;
                    let replaceWord=this.addData.changeWord;
                     // 如果是添加弹窗
                    if (this.addData.status==1) {
                        addSensitiveWords(sensitiveWord, replaceWord).then(res => {
                            if (res.data.status==200) {
                                this.$message({
                                    type: 'success',
                                    message: '添加成功!'
                                });
                                this.handleClose();
                                this.$emit('searchList');
                            } else {
                                let warnMessage=res.data.errorMsg;
                                this.$message({
                                    type: 'info',
                                    message: warnMessage
                                });
                            }
                        })
                    }
                    // 如果是修改弹窗
                    if (this.addData.status==2) {
                        let pId=this.addData.pId;
                        updateSensitiveWords(sensitiveWord, replaceWord, pId).then(res => {
                            this.$emit('searchList');
                            if (res.data.status==200) {
                                //console.log(res.data.status)
                                this.$message({
                                    type: 'success',
                                    message: '修改成功!'
                                });
                                this.handleClose();
                                this.$emit('searchList');
                            } else {
                                let warnMessage=res.data.errorMsg;
                                this.$message({
                                    type: 'info',
                                    message: warnMessage
                                });
                            }
                        });
                    }
                } 
            });
        },
        // 弹出框的关闭按钮
        handleClose(done) {
            this.$refs.addData.resetFields();
            this.delDialogAdd();
        },
        resetFormdelDialogAdd(addDate) {
            this.$refs[addDate].resetFields();
            this.delDialogAdd();
        },
        // 还原弹窗信息
        delDialogAdd() {
            this.addData.dialogAdd = false;
            this.addData.popTitle='';
            this.addData.status=1;
            this.addData.word='';
            this.addData.changeWord='';
            this.addData.pId=''
        }
    }
}
</script>

<style lang="scss" scoped>
@import '../../style/dialog.scss';
</style>

